package com.ml.loan.common.valid;

import cn.hutool.core.util.ReUtil;
import org.apache.commons.lang3.StringUtils;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;

/**
 * 中文姓名校验器
 *
 * @Author caozx
 * @Date 2021/4/9 19:57
 **/
public class IsChineseNameValidator implements ConstraintValidator<IsChineseName, String> {

    private boolean required = false;

    @Override
    public void initialize(IsChineseName constraintAnnotation) {
        required = constraintAnnotation.required();
    }

    @Override
    public boolean isValid(String value, ConstraintValidatorContext constraintValidatorContext) {
        if (StringUtils.isBlank(value)) {
            return !required;
        }
        //中文姓名，支持少数名族(例：沙迪克·达吾提)
        return ReUtil.isMatch("[\u4E00-\u9FA5]{2,10}(?:·[\u4E00-\u9FA5]{2,10})*", value);
    }
}
